'use strict';
const mongoose = require('mongoose');
const config = require('../config/config.unittest.js')({});
const fs = require('fs');
const readline = require('readline');
const moment = require('moment');

let ParserBetter = require('../app/utils/rss/parser');
let parserBetter = new ParserBetter({timeout: 10000});

const UserModel = require('../app/model/User')({
  mongoose,
});
const TopicModel = require('../app/model/Topic')({
  mongoose,
});

mongoose.connect(config.mongoose.url, config.mongoose.options, function (err) {
  if (err) {
    console.error('connect to %s error: ', config.mongoose, err.message);
    process.exit(1);
  }
});

async function main() {
  const old_name = '观察者';
  const new_name = '观察者网';

  let day = moment().subtract(7, 'days');
  const r1 = await TopicModel.remove({tab: 'hot', author_name: old_name, create_at: {$lt: day}});
  console.log(r1.result);

  const r2 = await TopicModel.updateMany(
    {author_name: old_name},
    {$set: {author_name: new_name}}
  );

  const r3 = await UserModel.updateMany(
    {loginname: old_name},
    {$set: {loginname: new_name, nickname: new_name}}
  );

  const r4 = await UserModel.updateMany(
    {'followings.name': old_name},
    {$set: {'followings.$.name': new_name}}
  );



  console.log(r2);
  console.log(r3);
  console.log(r4);

  process.exit(0);
}

main();

